package cn.fudong.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.fudong.bos.domain.base.Area;
import cn.fudong.bos.domain.constant.Constants;
import cn.fudong.bos.domain.take_delivery.Order;
import cn.fudong.crm.domain.Customer;

/**
 * 
 * @description: bos_fore用户订单管理
 * @author ：d_fu
 * @date 创建时间：2018年3月23日 下午7:08:18
 *
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {

	// 属性驱动接收sendAreaInfo recAreaInfo
	private String sendAreaInfo;
	private String recAreaInfo;

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	// 添加订单操作
	@Action(value = "order_add", results = {
			@Result(name = "success", type = "redirect", location = "index.html") })
	public String add() {
		// 解析省市区信息，存入Order中的Area
		String[] sendAreaData = sendAreaInfo.split("/");
		Area SendArea = new Area();
		SendArea.setProvince(sendAreaData[0]);
		SendArea.setCity(sendAreaData[1]);
		SendArea.setDistrict(sendAreaData[2]);

		String[] recAreaDate = recAreaInfo.split("/");
		Area RecArea = new Area();
		RecArea.setProvince(recAreaDate[0]);
		RecArea.setCity(recAreaDate[1]);
		RecArea.setDistrict(recAreaDate[2]);

		model.setSendArea(SendArea);
		model.setRecArea(RecArea);

		// 拿到customer_id
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		model.setCustomer_id(customer.getId());

		// 将数据通过webService发到bos_management中
		WebClient.create(Constants.BOS_MANAGEMENT_URL + "/services/orderService/order").type(MediaType.APPLICATION_JSON)
				.post(model);

		return SUCCESS;

	}

}
